package com.wgts.wgts.mvp.view.activity.pay;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.PlatformPayData;
import com.wgts.wgts.control.dialog.PlatformPayDialogFragment;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerContinuePayActivityComponent;
import com.wgts.wgts.dagger.module.ContinuePayActivityModule;
import com.wgts.wgts.localbean.PayData;
import com.wgts.wgts.mvp.presenter.IContinuePayActivityPresenter;
import com.wgts.wgts.mvp.view.IContinuePayActivityView;
import com.wgts.wgts.mvp.view.activity.affrimorder.AffirmOrderActivity;
import com.wgts.wgts.mvp.view.activity.login.LoginActivity;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.mvp.view.activity.setPassWord.SettingPasswordActivity;
import com.wgts.wgts.netmodle.resp.OrderEntity;
import com.wgts.wgts.netmodle.resp.UserInfoEntity;
import com.wgts.wgts.thirdParty.pay.PayTools;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2215:39
 *
 */public class ContinuePayActivity extends BaseActivity implements IContinuePayActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.wx_pay_image)
    ImageView wxPayImage;
    @BindView(R.id.wx_pay_ly)
    LinearLayout wxPayLy;
    @BindView(R.id.alipay_pay_image)
    ImageView alipayPayImage;
    @BindView(R.id.alipay_pay_ly)
    LinearLayout alipayPayLy;
    @BindView(R.id.affirm_order_residue_money)
    TextView affirmOrderResidueMoney;
    @BindView(R.id.mohu_pay_image)
    ImageView mohuPayImage;
    @BindView(R.id.mohu_pay_ly)
    LinearLayout mohuPayLy;
    @BindView(R.id.affirm_order_go_pay_price)
    TextView affirmOrderGoPayPrice;
    @BindView(R.id.affirm_order_go_pay_button)
    LinearLayout affirmOrderGoPayButton;

    //支付方式0微信，1支付宝，2
    private Integer choosePay = 1;
    private String payType = "ALIPAY";
    private PlatformPayData data;

    private String orderId, SneedPayPrice, SplatformCoin, SorderType;
    public static final String ORDER_ID = "orderId";
    public static final String NEED_PAY_PRICE = "needPayPrice";

    public static final int PAY_RESULT_OK = 1123;
    private PayData payData;

    @Inject
    IContinuePayActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.continue_pay_activity);
        ButterKnife.bind(this);
        //初始设置不可使用
        mohuPayImage.setImageResource(R.mipmap.no_pay_check);
        mohuPayLy.setClickable(false);

        includeTitle.setText("继续支付");
        Intent intent = getIntent();
        payData = intent.getParcelableExtra(PayTools.PAY_DATE);
        orderId = payData.orderId;
        SneedPayPrice = payData.needPayPrice;
        SorderType = payData.orderType;
        affirmOrderGoPayPrice.setText("￥" + SneedPayPrice);
        alipayPayImage.setSelected(true);
        presenter.userInfo();
    }

    @Override
    public void init() {
        DaggerContinuePayActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .continuePayActivityModule(new ContinuePayActivityModule(this))
                .build()
                .inject(this);
    }

    @OnClick({R.id.back_btn_ly, R.id.wx_pay_ly, R.id.alipay_pay_ly, R.id.mohu_pay_ly, R.id.affirm_order_go_pay_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.wx_pay_ly:
                choosePayType(0);
                break;
            case R.id.alipay_pay_ly:
                choosePayType(1);
                break;
            case R.id.mohu_pay_ly:
                choosePayType(2);
                break;
            case R.id.affirm_order_go_pay_button:
                if (choosePay == 2){
                    if (AppInfor.getInstance().isModifyPayPassword()){
                        platformPay();
                    }else {
                        settingPassword();
                    }
                }else {
                    showDialog();
                    presenter.continuePay(orderId, payType, "");
                }
                break;
        }
    }

    /*
     * 选择支付方式
     * */
    private void choosePayType(Integer type) {
        switch (type) {
            case 0:
                choosePay = 0;
                payType = "WXPAY";
                wxPayImage.setSelected(true);
                alipayPayImage.setSelected(false);
                mohuPayImage.setSelected(false);
                break;
            case 1:
                choosePay = 1;
                payType = "ALIPAY";
                wxPayImage.setSelected(false);
                alipayPayImage.setSelected(true);
                mohuPayImage.setSelected(false);
                break;
            case 2:
                choosePay = 2;
                payType = "BALANCEPAY";
                wxPayImage.setSelected(false);
                alipayPayImage.setSelected(false);
                mohuPayImage.setSelected(true);
                break;
            default:
        }
    }
    //支付方式
    private void platformPay(){
        data = new PlatformPayData();
        //data.setOrderInfo("订单信息，支付业务安全信息");
        data.setPayPrice(SneedPayPrice);
        data.setResidueCoin("账户余额：" + SplatformCoin + "元");
        PlatformPayDialogFragment dialogFragment = PlatformPayDialogFragment.getPlatformPayDialogFragment(data);
        dialogFragment.show(getSupportFragmentManager(), "platformPayDialog");
        dialogFragment.setPlatformPay(new PlatformPayDialogFragment.platformPay() {
            @Override
            public void platformPay(String password) {
                showDialog();
                presenter.continuePay(orderId, payType, password);
            }
        });
    }

    //设置支付密码
    private void settingPassword(){
        ToastDate toastDate = new ToastDate();
        toastDate.setCancelQuestion("请先设置资金密码");
        toastDate.setCancelText("取消");
        toastDate.setPointText("去设置");
        final ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
        fragment.show(getSupportFragmentManager(), "SettingPasswordDialog");
        fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
            @Override
            public void onClickCancle() {
                Intent intent = new Intent(ContinuePayActivity.this, SettingPasswordActivity.class);
                startActivity(intent);
                fragment.dismiss();
            }

            @Override
            public void onClickEnsuer() {
                fragment.dismiss();
            }
        });
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void continuePay(OrderEntity orderEntity) {
        dialogDismiss();
        switch (payType) {
            case "WXPAY":
                PayTools.getInstance().pay(0, orderEntity.orderPayParam, this, payData);
                break;
            case "ALIPAY":
                PayTools.getInstance().pay(1, orderEntity.orderPayParam, this, payData);
                break;
            case "BALANCEPAY":
                Intent intent = new Intent(ContinuePayActivity.this, PaySuccessActivity.class);
                intent.putExtra(PayTools.PAY_DATE, payData);
                startActivity(intent);
                Intent intent1 = new Intent();
                intent1.putExtra("PayStatue", "success");
                setResult(ContinuePayActivity.PAY_RESULT_OK, intent1);
                finish();
                break;
        }
    }

    @Override
    public void userInfoResult(UserInfoEntity entity) {
        if(entity == null){
            Intent intent = new Intent(ContinuePayActivity.this, LoginActivity.class);
            startActivity(intent);
        }else {
            SplatformCoin = AppInfor.getInstance().getAvailableBalance();
            Double needPayPrice = Double.parseDouble(SneedPayPrice);
            Double platformCoin = Double.parseDouble(SplatformCoin);
            affirmOrderResidueMoney.setText("账户余额：￥" + SplatformCoin + "元");
            if (platformCoin < needPayPrice) {
                mohuPayImage.setImageResource(R.mipmap.no_pay_check);
                mohuPayLy.setClickable(false);
            } else {
                mohuPayImage.setImageResource(R.drawable.pay_check);
                mohuPayLy.setClickable(true);
            }
        }
    }

}
